/**
 * $Id: CMPPCommon.java,v 1.4 2006/11/30 08:17:32 martin Exp $
 *
 * Copyright 2005 Skyinfo, Inc. All Rights Reserved.
 */
package com.haoxi.gateway.cmppmsg;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.haoxi.gateway.util.TypeConvert;

public class CMPPCommon {

    public static final int CMPP_CONNECT = 0x00000001;
    public static final int CMPP_CONNECT_RESP = 0x80000001;
    public static final int CMPP_TERMINATE = 0x00000002;
    public static final int CMPP_TERMINATE_RESP = 0x80000002;
    public static final int CMPP_SUBMIT = 0x00000004;
    public static final int CMPP_SUBMIT_RESP = 0x80000004;
    public static final int CMPP_DELIVER = 0x00000005;
    public static final int CMPP_DELIVER_RESP = 0x80000005;
    public static final int CMPP_ACTIVE = 0x00000008;
    public static final int CMPP_ACTIVE_RESP = 0x80000008;

    private static int sequence = 0;
    
    public static final int MSG_HEAD_LENGTH = 12;

    public static String getMsgIdStr(byte[] buf) {
//         long lontmp = TypeConvert.byte2long(buf);
//         String tmp = "";
//         tmp = Long.toBinaryString(lontmp);
//         // 不足64位前补0操作
//         if (tmp.length() < 64) {
//         String tmp0 = "000";
//         tmp = tmp0.substring(0, 64 - tmp.length()) + tmp;
//         }
//         StringBuffer temp = new StringBuffer();
//         // 对二进制串进行分割后得到最终需要的字符串
//         int month = Integer.parseInt(tmp.substring(0, 4), 2) - 1;
//         int day = Integer.parseInt(tmp.substring(4, 9), 2);
//         int hour = Integer.parseInt(tmp.substring(9, 14), 2);
//         int min = Integer.parseInt(tmp.substring(14, 20), 2);
//         int sec = Integer.parseInt(tmp.substring(20, 26), 2);
//         int gatewaycode = Integer.parseInt(tmp.substring(26, 48), 2);
//         int seq = Integer.parseInt(tmp.substring(48), 2);
//         Calendar calendar = Calendar.getInstance();
//         calendar.set(Calendar.MONTH, month);
//         calendar.set(Calendar.DATE, day);
//         calendar.set(Calendar.HOUR_OF_DAY, hour);
//         calendar.set(Calendar.MINUTE, min);
//         calendar.set(Calendar.SECOND, sec);
//         SimpleDateFormat dateformat = new SimpleDateFormat("MMddHHmmss");
//         Date date = calendar.getTime();
//         tmp = dateformat.format(date);
//         temp.append(tmp).append(gatewaycode).append(seq);
//         return temp.toString();
         String as[] = new String[1];
         int ai[] = new int[1];
         DecodeCMPPMsgID(buf, ai, as);
                return as[0];

    }
    public static int Bytes4ToInt ( byte mybytes[] ) {
        return ( 0xff & mybytes[ 0 ] ) << 24 | ( 0xff & mybytes[ 1 ] ) << 16 | ( 0xff & mybytes[ 2 ] ) << 8 | 0xff & mybytes[ 3 ];
    }
    public static long getLongByBytesBe(byte abyte0[]) {
        int i = abyte0.length >= 8 ? 8 : abyte0.length;
        long l = 0L;
        for (int j = 0; j < i; j++) {
            l <<= 8;
            l |= 255L & (long) abyte0[j];
        }

        return l;
    }

    public static int DecodeCMPPMsgID(byte abyte0[], int ai[], String as[]) {
        StringBuffer stringbuffer = new StringBuffer();
        as[0] = new String();
        if (abyte0.length != 8)
            return -1;
        long l1 = getLongByBytesBe(abyte0);
        int k1 = (int) (l1 & 65535L);
        l1 >>>= 16;
        int i = (int) (l1 & 0x3fffffL);
        l1 >>>= 22;
        int j1 = (int) (l1 & 63L);
        l1 >>>= 6;
        int i1 = (int) (l1 & 63L);
        l1 >>>= 6;
        int l = (int) (l1 & 31L);
        l1 >>>= 5;
        int k = (int) (l1 & 31L);
        l1 >>>= 5;
        int j = (int) (l1 & 15L);
        if (j1 >= 60) {
            i1 += j1 / 60;
            j1 %= 60;
        }
        if (i1 >= 60) {
            l += i1 / 60;
            i1 = j1 % 60;
        }
        if (l >= 24) {
            k += l / 24;
            l %= 24;
        }
        stringbuffer.setLength(0);
        ai[0] = k1;
        stringbuffer.append(FormatInt(j, 2));
        stringbuffer.append(FormatInt(k, 2));
        stringbuffer.append(FormatInt(l, 2));
        stringbuffer.append(FormatInt(i1, 2));
        stringbuffer.append(FormatInt(j1, 2));
        stringbuffer.append(FormatInt(i, 5));
        stringbuffer.append(FormatInt(k1, 5));
        as[0] = stringbuffer.toString();
        return 0;
    }

    private static String FormatInt(int i, int j) {
        String s;
        for (s = String.valueOf(i); s.length() < j; s = "0" + s)
            ;
        if (s.length() > 5)
            s = s.substring(0, 5);
        return s;
    }

    public synchronized static void setMobileSequence(CMPPMessage message) {

        sequence++;
        if (sequence >= 0x7fffffff) {
            sequence = 0;
        }
        message.setSequenceId(sequence);
    }
    public static void main(String[] args )
    {
        
        byte[] test=new byte[8];
        test[0] = -105;
        test[1] = 34;
        test[2] = 49;
        test[3] = -128;
        test[4] = 7;
        test[5] = -47;
        test[6] = 82;
        test[7] = 34;
       String str= getMsgIdStr(test);
       System.out.println(str);
        
        
        
    }
}
